import MComponent from '../../common/MComponent'
import { _appoint } from '../../api/projects'
import { _getPhoneNumber } from '../../api/getuserinfo'
import { NAME_REG, TEL_REG } from '../../utils/reg'
const app = getApp()
const day = new Date()
day.setTime(day.getTime() + 24 * 60 * 60 * 1000)
const today = day.getFullYear() + '-' + (day.getMonth() + 1) + '-' + day.getDate()
MComponent({
  data: {
    uid: '',
    id: '',
    name: '',
    tel: '',
    date: today,
    today,
    success: false,
    result: null
  },
  methods: {
    onInput (e) {
      const { attr } = e.currentTarget.dataset
      const { value } = e.detail
      this.data[attr] = value
    },
    onChange (e) {
      const { attr } = e.currentTarget.dataset
      const { value } = e.detail
      this.set({
        [attr]: value
      })
    },
    getPhoneNumber (e) {
      const { iv, encryptedData } = e.detail
      const OpenID = wx.getStorageSync('openid')
      const UnionID = wx.getStorageSync('uid')
      if (iv && encryptedData) {
        app.loading('加载中')
        _getPhoneNumber({
          OpenID, UnionID,iv, encryptedData
        })
          .then(res => {
            wx.hideLoading()
            const { code, msg, data } = res.data
            if (code === 0) {
              this.set({
                tel: data.purePhoneNumber
              })
            } else {
              wx.showModal({
                title: '对不起',
                content: msg,
                showCancel: false
              })
            }
          })
          .catch(err => {
            wx.hideLoading()
            console.log(err)
            wx.showModal({
              title: '对不起',
              content: err.toString(),
              showCancel: false
            })
          })
      }
    },
    submit () {
      const UnionID = wx.getStorageSync('uid')
      // const FansID = member.ID
      let { id: ProjectID, name: Name, tel: Tel, date: AppointTime } = this.data
      AppointTime = AppointTime.replace(/\-/g, '/')
      if (!NAME_REG.test(Name)) {
        app.toast('请填写正确的中文姓名')
        return
      }
      if (!TEL_REG.test(Tel)) {
        app.toast('请先获取手机号码')
        return
      }
      if (!AppointTime) {
        app.toast('请选择预约日期')
        return
      }
      app.loading('提交中')
      _appoint({ ProjectID, UnionID, Name, Tel, AppointTime })
        .then(res => {
          wx.hideLoading()
          const { code, msg, data } = res.data
          if (code === 0) {
            this.set({
              success: true,
              result: data
            })
          } else if (code === 2) {
            wx.showModal({
              title: '温馨提示',
              content: msg,
              showCancel: false,
              success: r => {
                if (r.confirm) {
                  console.log(2)
                  wx.redirectTo({
                    url: './appoint-record'
                  })
                }
              }
            })
          } else {
            wx.showModal({
              title: '对不起',
              content: msg,
              showCancel: false
            })
          }
        })
        .catch(err => {
          console.log(err)
          wx.hideLoading()
        })
    },
    onLoad (opt) {
      const { id } = opt
      this.data.id = id
      app.loading('加载中')
      app.login()
      app.checkAuth()
        .then(res => {
          const uid = res
          this.set({
            uid
          })
          return app.getUserInfoByUid(uid)
        })
        .then(memberInfo => {
          this.set({
            member: memberInfo
          })
          if (memberInfo.FansTel) {
            this.set({
              tel: memberInfo.FansTel
            })
          }
          wx.hideLoading()
        })
        .catch(err => {
          wx.hideLoading()
          console.log(err)
        })
    }
  }
}, true)